﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class UIERK : SDBASE
    {
    
        #region ==V2===========
        public static readonly DependencyProperty V2Property = DependencyProperty.Register("V2",
            typeof(string), typeof(UIERK),
            new FrameworkPropertyMetadata("00", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnV2Changed));
        public string V2
        {
            get
            {
                return (string)GetValue(V2Property);
            }
            set
            {
                SetValue(V2Property, value);
                OnV2Chg();
            }
        }

        private static void OnV2Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIERK;
            if (src != null)
            {
                src.V2 = (string)e.NewValue;
            }
        }

        public virtual void OnV2Chg()
        {
        }

        #endregion

        #region ==V3===========
        public static readonly DependencyProperty V3Property = DependencyProperty.Register("V3",
            typeof(string), typeof(UIERK),
            new FrameworkPropertyMetadata("00", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnV3Changed));
        public string V3
        {
            get
            {
                return (string)GetValue(V3Property);
            }
            set
            {
                SetValue(V3Property, value);
                OnV3Chg();
            }
        }

        private static void OnV3Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIERK;
            if (src != null)
            {
                src.V3 = (string)e.NewValue;
            }
        }

        public virtual void OnV3Chg()
        {
        }

        #endregion
        #region ==V4===========
        public static readonly DependencyProperty V4Property = DependencyProperty.Register("V4",
            typeof(string), typeof(UIERK),
            new FrameworkPropertyMetadata("00", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnV4Changed));
        public string V4
        {
            get
            {
                return (string)GetValue(V4Property);
            }
            set
            {
                SetValue(V4Property, value);
                OnV4Chg();
            }
        }

        private static void OnV4Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIERK;
            if (src != null)
            {
                src.V4 = (string)e.NewValue;
            }
        }

        public virtual void OnV4Chg()
        {
        }

        #endregion

        #region ==V5===========
        public static readonly DependencyProperty V5Property = DependencyProperty.Register("V5",
            typeof(string), typeof(UIERK),
            new FrameworkPropertyMetadata("00", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnV5Changed));
        public string V5
        {
            get
            {
                return (string)GetValue(V5Property);
            }
            set
            {
                SetValue(V5Property, value);
                OnV5Chg();
            }
        }

        private static void OnV5Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIERK;
            if (src != null)
            {
                src.V5 = (string)e.NewValue;
            }
        }

        public virtual void OnV5Chg()
        {
        }

        #endregion

        #region ==IsChkAll===========
        public static readonly DependencyProperty IsChkAllProperty = DependencyProperty.Register("IsChkAll",
            typeof(bool), typeof(UIERK),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsChkAllChanged));
        public bool IsChkAll
        {
            get
            {
                return (bool)GetValue(IsChkAllProperty);
            }
            set
            {
                SetValue(IsChkAllProperty, value);
            }
        }
        private static void OnIsChkAllChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIERK;
            if (src != null)
            {
                src.IsChkAll = (bool)e.NewValue;
            }
        }
        #endregion
        #region ==EKEY===========
        public static readonly DependencyProperty EKEYProperty = DependencyProperty.Register("EKEY",
            typeof(string), typeof(UIERK),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEKEYChanged));
        public string EKEY
        {
            get
            {
                return (string)GetValue(EKEYProperty);
            }
            set
            {
                SetValue(EKEYProperty, value);
            }
        }
        private static void OnEKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIERK;
            if (src != null)
            {
                src.EKEY = (string)e.NewValue;
            }
        }
        #endregion
        #region ==IsFinish===========
        public static readonly DependencyProperty IsFinishProperty = DependencyProperty.Register("IsFinish",
            typeof(bool), typeof(UIERK),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsFinishChanged));
        public bool IsFinish
        {
            get
            {
                return (bool)GetValue(IsFinishProperty);
            }
            set
            {
                SetValue(IsFinishProperty, value);
            }
        }
        private static void OnIsFinishChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIERK;
            if (src != null)
            {
                src.IsFinish = (bool)e.NewValue;
            }
        }
        #endregion
        public UIERK()
        {
          
            
        }
        public void SetUp()
        {
            TMV.SetUp(SDSA);
        }
       
        public override void LOADALL()
        {
            TMV.SetUp(SDSA);
            base.LOADALL(); 
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                V1 = "00";
                V2 = "00";
                V3 = "00";
                V4 = "00";
                V5 = "00";

            }
            else
            {
                V1 = dwMaster["V1"].ToString();
                V2 = dwMaster["V2"].ToString();
                V3 = dwMaster["V3"].ToString();
                V4 = dwMaster["V4"].ToString();
                V5 = dwMaster["V5"].ToString();
            }

        }

        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (SDSA == null)
                return;
            if (SDSA.Trim() == "")
                return;
            TMV.SetUp(SDSA);
            dwData = TBMain.NewRow();
            dwData["SDSA"] = SDSA;
            dwData["SDKEY"] = " AND SDKEY LIKE '%" + LMS + "'";
            SResult = TMV.QuerySDDAT(dwData, System.Convert.ToInt32(SDSA));
            MergeData(SResult);
            base.QueryCommand(sender, e);
        }

        public override void NewCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (KEY.Trim().Length == 0)
            {
                MessageBox.Show("需要先键入新键值");
                return;
            }
            try
            {
                DataRow dwData;
                dwData = TBMain.NewRow();
                TMV.SetUpNewRow(ref dwData, SDSA);
                dwData["KEY"] = KEY;
                dwData["VALUE"] = " ".PadRight(20);
                dwData["V1"] = "00";
                dwData["V2"] = "00";
                dwData["V3"] = "00";
                dwData["V4"] = "00";
                dwData["V5"] = "00";
                TBMain.Rows.InsertAt(dwData, 0);
                vwMaster.View.MoveCurrentToFirst();
                base.NewCommand(sender, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show("请尝试查询一下再新建");
            }
        }
        
        public override void DeleteCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DeleteSDDAT(dwMaster, 1);
            string sdkey = dwMaster["SDKEY"].ToString();
            vwMaster.View.MoveCurrentTo(-1);
            string filter = "SDKEY='" + sdkey + "'";
            DataRow[] rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }

        public override void CopyCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (KEY.Trim().Length == 0)
            {
                MessageBox.Show("需要先键入新键值");
                return;
            }
            if (KEY.Trim().Length>3 )
            {
                MessageBox.Show("键值长度为3");
                return;
            }
            if (dwMaster == null)
                return;
            DataRow dwData;
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            dwData["KEY"] = KEY;
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
            base.CopyCommand(sender, e);
        }

        public override void SaveCommand(object sender, System.Windows.RoutedEventArgs e)
        {

            try
            {
                if (dwMaster == null)
                    return;
                dwMaster["VALUE"] = dwMaster["VALUE"].ToString().Substring(0, 8) + V1.PadRight(2) + V2.PadRight(2) + V3.PadRight(2) + V4.PadRight(2) + V5.PadRight(2) + dwMaster["VALUE"].ToString().Substring(18);
                TMV.Save(dwMaster);
                base.SaveCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
    }
}
